package www.dianbaozu.com.ui.server

import android.text.InputFilter
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.netease.nim.uikit.api.NIMHelper
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentChangingServerNameBinding
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.data.entities.ServerInfo
import www.wen.com.common.lib.data.entities.ServerUpdateInfoEntity
import www.wen.com.common.lib.extension.getColorAgency
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.KeyHelper.UPDATE_SERVER_INFORMATION_KEY
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter


/**
 * @author WEN
 * @Description:修改用户在服务器中的昵称
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
open class ChangingUseNameInServerFragment : BaseLoadingFragment<FragmentChangingServerNameBinding>(), View.OnClickListener {

    val mUpdateServerInfoViewModel: UpdateServerInfoViewModel by viewModels()

    lateinit var mServerInfo: ServerInfo

    override val viewModel: UpdateServerInfoViewModel
        get() = mUpdateServerInfoViewModel

    override fun isEnableLoadingHelper() = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
            requireActivity(),
            tilte = getTitleName(),
            isShowRightText = true,
            rightText = geRightText(),
            rightTextColor = getColorAgency(R.color.colorFFFFFF),
            action = ::titleAction
    )

    open fun getTitleName() = getString(R.string.modify_nickname)

    open fun geRightText() = getString(R.string.com_save)

    override fun getLayoutId(): Int = R.layout.fragment_changing_server_name

    override fun lazyInitView() {
        dataBinding.run {
            mServerInfo = requireArguments().getParcelable(BUNDLE_DATA)!!
            listener = this@ChangingUseNameInServerFragment
            tvCreateGroup.text = getCreateGroupText()
            etPleaseEnterAGroupName.setText(getName())
            etPleaseEnterAGroupName.hint = getCreateGroupText1()
            tvErrorTip.isVisible = false
        }
        dataBinding.etPleaseEnterAGroupName.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(16))
    }

    open fun getName(): CharSequence = mServerInfo.serverMember!!.nickName

    open fun getCreateGroupText(): CharSequence = getString(R.string.modify_server)

    open fun getCreateGroupText1(): CharSequence = getString(R.string.please_server_nickname)

    open fun getCheckText(): String = getString(R.string.nicknames_cannot_empty)

    override fun actionRight() {
        val nickName = dataBinding.etPleaseEnterAGroupName.trimProxy()
        if (nickName.isBlank()) {
            toast(getCheckText())
            return
        }
        mUpdateServerInfoViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                tipSuccess = "修改昵称「${nickName}」成功",
                isShowToastTip = true,
                resultFailCall = {
                    mUpdateServerInfoViewModel.mIsShowErrorTip.set(View.VISIBLE)
                    mUpdateServerInfoViewModel.mErrorTipText.set(it)
                },
                request = { appDataManage ->
                    appDataManage.requestMemberSetting(
                            id = mServerInfo.serverMember!!.id.toString(),
                            RequestBodyParameter(
                                nickName = nickName,
                                serverId = mServerInfo.serverId,
                            ))
                }
        ) {
            mServerInfo.teamIds?.forEach { teamId ->
                NIMHelper.updateMyTeamNick(teamId, nickName)
            }
            toast("修改昵称「${nickName}」成功")

            postObject(UPDATE_SERVER_INFORMATION_KEY,ServerUpdateInfoEntity(serverId = mServerInfo.serverId,nickNameInServer = nickName))
            actionLeft()
        }

    }

    override fun onClick(v: View) {
        mUpdateServerInfoViewModel.mIsShowErrorTip.set(View.INVISIBLE)
    }

}



